package com.cykj.page04;

import java.util.HashMap;

/**
 * @BelongsProject: MyTomcat
 * @BelongsPackage: com.cykj.page02
 * @Author: WWJ
 * @CreateTime: 2025-06-23 17:03
 * @Description: TODO
 * @Version: 1.0
 */
public class MyHttpRequest {
    /*
     * 属性:
     *      请求方法  get post
     *      url      /login?account=zhangshan&password=123
     *               /123.png
     *      请求模块
     *      协议版本
     *      参数列表==》map
     *      请求头列表==》map
     *      请求体
     *
     * 三个方法  解析    请求行 请求头 请求体
     * */

    //请求方法
    private String requestMethod;
    //请求URL
    private String requestURL;
    //请求模块
    private String requestModel;
    //协议版本
    private String protocol;
    //请求参数
    private HashMap<String, String> requestParameters = new HashMap<>();
    //请求头参数
    private HashMap<String, String> requestHeadersParams = new HashMap<>();
    //请求体
    private String requestBody;
    //请求行
    private String requestLine;
    //请求消息
    private String requestMsg;

    //构造方法获取请求消息
    public MyHttpRequest(String requestMsg) {
        this.requestMsg = requestMsg;
        //解析消息
        parseRequestMessage();
    }

    //解析请求消息
    private void parseRequestMessage() {
        String[] splitRequestMsgArray = requestMsg.split("\r\n");
        requestLine = splitRequestMsgArray[0];//请求行
        //切割请求行
        parseRequestLine();

        //获取请求体
        if (requestMethod.equals("POST")) {
            //请求体
            requestBody = splitRequestMsgArray[splitRequestMsgArray.length - 1];
            parseRequestBody();
        }

        //解析请求头
        parseRequestHead(splitRequestMsgArray);
    }

    //解析请求行
    private void parseRequestLine() {
        String[] splitRequestLineArray = requestLine.split(" ");
        requestMethod = splitRequestLineArray[0];
        requestURL = splitRequestLineArray[1];
        protocol = splitRequestLineArray[2];
        //判断是否为GET请求
        if (requestMethod.equals("GET")) {
            if (requestURL.contains("?")) {
                String[] splitRequestURLArray = requestURL.split("\\?", 2);
                requestModel = splitRequestURLArray[0];
                String[] split = splitRequestLineArray[1].split("&");
                for (String item : split) {
                    String[] value = item.split("=");
                    if (value.length == 2) {
                        requestParameters.put(value[0], value[1]);
                    }
                }
            }
        }
    }

    //解析请求头
    private void parseRequestHead(String[] splitRequestMsgArray) {
        for(int i = 1; i < splitRequestMsgArray.length; i++){
            String[] splitRequestHead = splitRequestMsgArray[i].split(": ");
            //有头 没有值 会报错 判断数组的长度大于1
            if (splitRequestHead.length >1){
                requestHeadersParams.put(splitRequestHead[0],splitRequestHead[1]);
            }
        }
        System.out.println(requestHeadersParams);
    }



    //post请求才有请求体
    private void parseRequestBody() {
        String[] split = requestBody.split("&");
        for (String item : split) {
            String[] splitItem = item.split("=");
            requestParameters.put(splitItem[0], splitItem[1]);
        }
    }

    public String getProtocol() {
        return protocol;
    }

    public String getRequestBody() {
        return requestBody;
    }

    public HashMap<String, String> getRequestHeadersParams() {
        return requestHeadersParams;
    }

    public String getRequestLine() {
        return requestLine;
    }

    public String getRequestMethod() {
        return requestMethod;
    }

    public String getRequestModel() {
        return requestModel;
    }

    public String getRequestMsg() {
        return requestMsg;
    }

    public HashMap<String, String> getRequestParameters() {
        return requestParameters;
    }

    public String getRequestURL() {
        return requestURL;
    }
}
